package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryPlanListDataListReq;
import com.sg.dto.base.app.req.QueryPlanDataDetailReq;
import com.sg.dto.base.app.req.QueryPlanDataListReq;
import com.sg.entity.OmsPlanning;
import com.sg.service.base.app.MOmsPlanningService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsPlanningMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsPlanningServiceImpl
        extends ServiceImpl<OmsPlanningMapper, OmsPlanning>
        implements MOmsPlanningService {

    @Autowired
    OmsPlanningMapper omsPlanningMapper;

    /**
     * 3-5-01查规划资料列表[2536]
     * gen by moon at 10/15/2022, 9:58:55 PM
     */
    @Trace(operationName = "3-5-01查规划资料列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsPlanning> queryPlanDataList(QueryPlanDataListReq reqDto) {

        try {
            QueryWrapper<OmsPlanning> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsPlanning.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsPlanning.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsPlanning.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsPlanning>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-5-01查规划资料详情[2515]
     * gen by moon at 4/24/2023, 12:54:06 AM
     */
    @Trace(operationName = "3-5-01查规划资料详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsPlanning queryPlanDataDetail(QueryPlanDataDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            QueryWrapper<OmsPlanning> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsPlanning> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsPlanning retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-5-01新增规划资料[2346]
     * gen by moon at 10/15/2022, 9:58:53 PM
     */
    @Trace(operationName = "3-5-01新增规划资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPlanData(OmsPlanning omsPlanning) {
        omsPlanning.setPlanningId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsPlanning.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsPlanning.setIsUpdate("TRUE");
        omsPlanning.setUpdateStatus("NEW");
        omsPlanning.setArchivngType("NOT_ARCHIVE");
        omsPlanning.setIsEditing("TRUE");
        omsPlanning.setIsArchive("FALSE");
        omsPlanning.setSubjectLifeCycle("EDITING");
        omsPlanning.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsPlanning.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsPlanning.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsPlanning.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsPlanning.setIsValid("FALSE");
        omsPlanning.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsPlanning.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsPlanning)) {
            return omsPlanning.getPlanningId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-5-01批量查询规划资料列表[2150]
     * gen by moon at 10/15/2022, 9:58:53 PM
     */
    @Trace(operationName = "3-5-01批量查询规划资料列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsPlanning> batchQueryPlanListDataList(BatchQueryPlanListDataListReq reqDto) {

        try {
            QueryWrapper<OmsPlanning> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getPlanningList() == null || reqDto.getPlanningList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("eva_obj_target_cycle_id", reqDto.getPlanningList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsPlanning.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsPlanning.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsPlanning.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsPlanning>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-5-01批量增规划资料[2895]
     * gen by moon at 10/15/2022, 9:58:55 PM
     */
    @Trace(operationName = "3-5-01批量增规划资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddPlanData(List<OmsPlanning> omsPlanning) {

        if (omsPlanning == null || omsPlanning.size() == 0) {
            return false;
        }

        return super.saveBatch(omsPlanning.stream().map(item -> {
            item.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
            item.setIsUpdate("TRUE");
            item.setUpdateStatus("NEW");
            item.setArchivngType("NOT_ARCHIVE");
            item.setIsEditing("TRUE");
            item.setIsArchive("FALSE");
            item.setSubjectLifeCycle("EDITING");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 3-5-01修改规划资料[2917]
     * gen by moon at 10/15/2022, 9:58:56 PM
     */
    @Trace(operationName = "3-5-01修改规划资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePlanData(OmsPlanning omsPlanning) {
        omsPlanning.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsPlanning != null && omsPlanning.getSubjectLifeCycle() != null && omsPlanning.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsPlanning.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsPlanning.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsPlanning != null && omsPlanning.getSubjectLifeCycle() != null && omsPlanning.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsPlanning.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsPlanning);
    }

    /**
     * 3-5-01批量改规划资料[2918]
     * gen by moon at 10/15/2022, 9:58:56 PM
     */
    @Trace(operationName = "3-5-01批量改规划资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdatePlanData(List<OmsPlanning> omsPlanning) {

        if (omsPlanning == null || omsPlanning.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsPlanning);

    }

    /**
     * 3-5-01批量删规划资料[3627]
     * gen by moon at 10/15/2022, 9:58:57 PM
     */
    @Trace(operationName = "3-5-01批量删规划资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePlanData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-5-01删除规划资料[4092]
     * gen by moon at 10/15/2022, 9:58:58 PM
     */
    @Trace(operationName = "3-5-01删除规划资料")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePlanData(String planningId) {

        return super.removeById(planningId);
    }
}
